
/******************************************************************************
 *
 *	PROJECT: Flynax Classifieds Software
 *	VERSION: 4.0
 *	LISENSE: FL87MPRYOA0R - http://www.flynax.com/license-agreement.html
 *	PRODUCT: Auto Classifieds
 *	DOMAIN: www.siyaradiali.com
 *	FILE: LIB.JS
 *
 *	This script is a commercial software and any kind of using it must be 
 *	coordinate with Flynax Owners Team and be agree to Flynax License Agreement
 *
 *	This block may not be removed from this file or any other files with out 
 *	permission of Flynax respective owners.
 *
 *	Copyrights Flynax Classifieds Software | 2012
 *	http://www.flynax.com/
 *
 ******************************************************************************/

var browseMapClass = function(){
	/**
	* configurations array
	**/
	this.config = new Array();
	
	/**
	* plugin phrases
	**/
	this.phrases = new Array();
	
	/**
	* markers
	**/
	this.markers = new Array();
	
	/**
	* markers mapping
	**/
	this.mapping = new Array();
	
	/**
	* local map search data
	**/
	this.localSearch = new Array();
	
	/**
	* reference to this object
	**/
	var self = this;
	
	/**
	* load map
	**/
	this.load = function(key){
		key = key ? '_'+key.replace(/_/gi, '') : '';
		/* hide map if markes doesn't exist */
		if ( this.markers.length <= 0 )
		{
			$('#bm_map'+key).parent().parent().hide();
			return;
		}
		
		$('#bm_map'+key).parent().parent().show();
		
		/* generate map */
		$('#bm_map'+key).flMap({
			addresses: self.markers,
			phrases: {
				hide: self.phrases['hide'],
				show: self.phrases['show'],
				notFound: self.phrases['notFound']
			},
			scrollWheelZoom: false,
			zoom: 12,
			alphabetMarkers: self.config['use_letters'],
			ready: self.handler,
			localSearch: self.localSearch
		});
	}
	
	/**
	* map handler
	*
	* @param object base - map object, see flMap jquery plugin
	*
	**/
	this.handler = function(base){
		this.attachInfo = new Array();
		
		for ( var i = 0; i < base.markers.length; i++ )
		{
			eval("google.maps.event.addListener(base.markers["+i+"], 'click', function(){ \
				self.attachInfo(base, "+i+", self.markers["+i+"][3]); \
			});");
		}
		
		/* map icon click handler */
		$('span.bm_icon').unbind('click').click(function(){
			flynax.slideTo($('#bm_map').length > 0 ? '#bm_map' : 'div.bm_maps:first');
			var id = $(this).attr('id').split('_')[1];
			self.attachInfo(base, self.mapping[id], id);
		});
	}
	
	/**
	* map handler
	*
	* @param object base - map object, see flMap jquery plugin
	* @param int marker_id - marker ID to attache info to
	* @param int listing_id - listing ID
	*
	**/
	this.attachInfo = function(base, marker_id, listing_id){
		// close all windows
		if ( base.infoWindows.length > 0 )
		{
			for ( var i=0; i < base.infoWindows.length; i++ )
			{
				base.infoWindows[i].close();
			}
		}
		
		// load/open window
		if ( !base.infoWindows[marker_id].flLoaded )
		{
			$.get(rlConfig['seo_url'], {request: 'browseMap', id: listing_id}, function(response){
				base.infoWindows[marker_id].setContent(response);
				base.infoWindows[marker_id].flLoaded = true;
				base.infoWindows[marker_id].open(base.map, base.markers[marker_id]);
			});
		}
		base.infoWindows[marker_id].open(base.map, base.markers[marker_id]);
	}
	
	/**
	* recently added listing reload
	*
	* @param string key - requested type key
	*
	**/
	this.recentlyReload = function(key){
		this.load(key);
	}
}

var browseMap = new browseMapClass();